﻿@inherits AntDesignTestBase
@code {
    record Person(int Id, string Name);
    List<Person> _persons = new()
    {
        new(1, "John"),
        new(2, "Lucy"),
        new(3, "Jack"),
        new(4, "Emily"),
    };

    [Fact]
    public void DefaultSelectWithoutAllowClear()
    {
        JSInterop.Setup<DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
            .SetResult(new DomRect());
        var cut = Render<Select<int, Person>>(
            @<AntDesign.Select DataSource="@_persons"
                               LabelName="@nameof(Person.Name)"
                               ValueName="@nameof(Person.Id)"
                               Value="0">
            </AntDesign.Select>);
        cut.Invoking(c => c.Find("span.ant-select-clear")).Should().Throw<ElementNotFoundException>();
    }

    [Fact]
    public void DefaultSelectWithAllowClear()
    {
        JSInterop.Setup<DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
            .SetResult(new DomRect());
        int? value = 1;
        var cut = Render<Select<int?, Person>>(
            @<AntDesign.Select
                 DataSource="@_persons"
                 ValueName="@nameof(Person.Id)"
                 LabelName="@nameof(Person.Name)"
                 @bind-Value="@value"
                 AllowClear/>
            );
        var clear = cut.Find("span.ant-select-clear");
        clear.Click();
        value.Should().Be(default(int?));
    }

    IEnumerable<string> _datasource = new List<string>{ "Lucy", "John", "Jack", "Emily" };

    [Fact]
    public void MultipleSelectWithoutAllowClear()
    {
        JSInterop.Setup<DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
            .SetResult(new DomRect());
        IEnumerable<string> values = new List<string>();
        var cut = Render<Select<string, string>>(
            @<Select
                 Mode="multiple"
                 DataSource="@_datasource"
                 @bind-Values="@values">
                <SelectOptions>
                    @foreach (var data in _datasource)
                    {
                        <SelectOption TItemValue="string" TItem="string" Value="@data" Label="@data"/>
                    }
                </SelectOptions>
            </Select>
            );
        cut.Invoking(c => c.Find("span.ant-select-clear")).Should().Throw<ElementNotFoundException>();
    }

    [Fact]
    public void MultipleSelectWithAllowClearAndEmptyValues()
    {
        JSInterop.Setup<DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
            .SetResult(new DomRect());
        IEnumerable<string> values = new List<string>();
        var cut = Render<Select<string, string>>(
            @<Select
                 Mode="multiple"
                 DataSource="@_datasource"
                 @bind-Values="@values"
                 AllowClear>
                <SelectOptions>
                    @foreach (var data in _datasource)
                    {
                        <SelectOption TItemValue="string" TItem="string" Value="@data" Label="@data"/>
                    }
                </SelectOptions>
            </Select>
            );
        cut.Invoking(c => c.Find("span.ant-select-clear")).Should().Throw<ElementNotFoundException>();
    }

    [Fact]
    public void MultipleSelectWithAllowClearAndNotEmptyValues()
    {
        JSInterop.Setup<DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
            .SetResult(new DomRect());
        IEnumerable<string> values = new List<string>{ "Lucy" };
        var cut = Render<Select<string, string>>(
            @<Select
                 Mode="multiple"
                 DataSource="@_datasource"
                 @bind-Values="@values"
                 AllowClear>
                <SelectOptions>
                    @foreach (var data in _datasource)
                    {
                        <SelectOption TItemValue="string" TItem="string" Value="@data" Label="@data"/>
                    }
                </SelectOptions>
            </Select>
            );
        cut.Render();
        var clear = cut.Find("span.ant-select-clear");
        clear.Click();
        values.Should().BeEquivalentTo(default);
    }
}